package com.wyzz.hr.exportfile;

import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;

import com.wyzz.hr.base.Constants;
import com.wyzz.hr.bo.IStatBO;
import com.wyzz.hr.pojo.Empsalaryadj;
import com.wyzz.hr.pojo.Statusconf;
import com.wyzz.hr.spring.SpringBeanFactory;

import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;

public class ExportSalaryAdj extends MutipleSheetExportHandler implements
		Constants {

	public ExportSalaryAdj(List results[], List titles[]) {
		super(results, titles);
	}

	protected void buildTitle() throws RowsExceededException, WriteException {
		WritableFont titleFont = new WritableFont(WritableFont.TIMES, 10,
				WritableFont.BOLD, true, UnderlineStyle.NO_UNDERLINE,
				Colour.RED);
		WritableCellFormat titleCellFormat = new WritableCellFormat(titleFont);
		writableSheets = new WritableSheet[titles.length];
		for (int i = 0; i < titles.length; i++) {
			writableSheets[i] = writableWorkbook.createSheet(titles[i].get(0)
					.toString(), i);
			int sheetTitleLength = titles[i].size() - 1;
			for (int j = 0; j < sheetTitleLength; j++)
				writableSheets[i].addCell(new Label(j, 0, titles[i].get(j + 1)
						.toString(), titleCellFormat));

		}

	}

	protected void buildBody() throws RowsExceededException, WriteException,
			IOException {
		WritableFont writableFont = new WritableFont(WritableFont.TIMES, 10,
				WritableFont.BOLD, false);
		WritableCellFormat defaultCellFormat = new WritableCellFormat(
				writableFont);
		int size = results.length;
		int itemsSize[] = new int[size];
		for (int i = 0; i < size; i++) {
			List list = results[i];
			itemsSize[i] = titles[i].size() - 10;
			for (int j = 0; j < list.size(); j++) {
				Empsalaryadj adj = (Empsalaryadj) list.get(j);
				adj.decryEmpSalaryAdj(adj);
				writableSheets[i].addCell(new Label(0, j + 1, adj.getEsaEmpno()
						.getEmpDistinctNo(), defaultCellFormat));
				writableSheets[i].addCell(new Label(1, j + 1, adj.getEsaEmpno()
						.getEmpName(), defaultCellFormat));
				writableSheets[i]
						.addCell(new Label(2, j + 1, adj.getEsaEmpno()
								.getEmpDeptNo().getDepartmentName(),
								defaultCellFormat));
				writableSheets[i].addCell(new Label(3, j + 1, adj.getEsaEmpno()
						.getEmpType().getEmptypeName(), defaultCellFormat));
				writableSheets[i].addCell(new Label(4, j + 1, adj.getEsaEmpno()
						.getEmpLocationNo().getLocationName(),
						defaultCellFormat));
				writableSheets[i].addCell(new Label(5, j + 1, formatDate(adj
						.getEsaCurEffDate()), defaultCellFormat));
				writableSheets[i].addCell(new Label(6, j + 1, adj
						.getEsaCurIncrRate() != null ? (new StringBuilder())
						.append(adj.getEsaCurIncrRate().toPlainString())
						.append("%").toString() : "", defaultCellFormat));
				writableSheets[i].addCell(new Label(7, j + 1, adj
						.getEsaComments(), defaultCellFormat));
				writableSheets[i].addCell(new Label(8, j + 1, getStatus(adj
						.getEsaStatus()), defaultCellFormat));
				writableSheets[i].addCell(new Label(9, j + 1, adj
						.getEsaJobgradeCur().getJobGradeName(),
						defaultCellFormat));
				writableSheets[i].addCell(new Label(10, j + 1, adj
						.getEsaJobgradePro().getJobGradeName(),
						defaultCellFormat));
				writableSheets[i].addCell(new Label(11, j + 1, adj
						.getEsaEsavIdCur().getEsavEsac().getEsacName(),
						defaultCellFormat));
				writableSheets[i].addCell(new Label(12, j + 1, adj
						.getEsaEsavIdPro().getEsavEsac().getEsacName(),
						defaultCellFormat));
				for (int acssist = 0; acssist < 48; acssist++)
					try {
						Class ownerClass = adj.getClass();
						int number = acssist + 1;
						Method getMethod = ownerClass.getMethod(
								(new StringBuilder()).append("getEsaColumn")
										.append(number).append("Cur")
										.toString(), new Class[0]);
						BigDecimal bd = (BigDecimal) getMethod.invoke(adj,
								new Object[0]);
						if (bd == null)
							writableSheets[i].addCell(new Label(13 + acssist,
									j + 1, "", defaultCellFormat));
						else
							writableSheets[i].addCell(new Label(13 + acssist,
									j + 1, bd.toPlainString(),
									defaultCellFormat));
					} catch (Exception e) {
						e.printStackTrace();
					}

				for (int acssist = 0; acssist < 48; acssist++)
					try {
						Class ownerClass = adj.getClass();
						int number = acssist + 1;
						Method getMethod = ownerClass.getMethod(
								(new StringBuilder()).append("getEsaColumn")
										.append(number).append("Pro")
										.toString(), new Class[0]);
						BigDecimal bd = (BigDecimal) getMethod.invoke(adj,
								new Object[0]);
						if (bd == null)
							writableSheets[i].addCell(new Label(61 + acssist,
									j + 1, "", defaultCellFormat));
						else
							writableSheets[i].addCell(new Label(61 + acssist,
									j + 1, bd.toPlainString(),
									defaultCellFormat));
					} catch (Exception e) {
						e.printStackTrace();
					}

			}

		}

		writableWorkbook.write();
	}

	private String getStatus(Integer intergerStatus) {
		IStatBO statBO = (IStatBO) SpringBeanFactory.getBean("statBO");
		Statusconf state = statBO.getStatusByConfigAndTable("empsalaryadj",
				intergerStatus.intValue());
		String status = "";
		if (state != null)
			status = state.getStatusconfDesc();
		return status;
	}

	public String getEmpStaus(Integer i) {
		switch (i.intValue()) {
		case 0: // '\0'
			return "\u8F9E\u804C";

		case 1: // '\001'
			return "\u5728\u804C";

		case 2: // '\002'
			return "\u505C\u85AA\u7559\u804C";

		case 3: // '\003'
			return "\u9000\u4F11";

		case 4: // '\004'
			return "\u8F9E\u9000";
		}
		return null;
	}
}
